<?php
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$url = $control->getLinkUrl(array('name' => '保存','id' => 'savejqMessage'),array(
    'messageId' => $messageId,
    'parentEventId' => $eventId,
    'postModel' => $modelClass));
$updateId = $control->getAjaxContainerId();
$messageNameField = 'message_name_' . $messageId;
$imageField = 'image_media_image_' . $messageId;
$descriptionField = 'message_description_' . $messageId;

$model->addField($messageNameField);
$model->addField($imageField);
$model->addField($descriptionField);

if ($messageId > 0) {
    $message = new BWMessage($messageId);

    $model->$messageNameField = $message->name;
    $model->$descriptionField = $message->description;

    $imageMedia = $message->getMediaManager()->getMedia('image');
    if ($imageMedia)
        $model->$imageField = $message->getMediaManager()->getMedia('image')->content;
}
?>

<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
        function send<?php echo $messageId; ?>() {

        var formElement = document.getElementById("message-form");
        var formData = new FormData(formElement);

        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(data);
//                alert(data);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function() {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>
<div data-role="header">
  <h1>旅游管理</h1>
  </div>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "message-form",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
?>
<div data-role="content" class="ui-content" role="main" style="padding:0px;">
    <div class="item">
          <?php echo "信息名称"; ?>
    </div>
        <?php echo $form->textArea($model, $messageNameField, array('placeholder' => '信息名称...', 'required' => true)); ?>
    <div class="item">
          <?php echo "信息缩略图"; ?>
    </div>
            <?php
            echo $form->fileField($model, $imageField);
            if ($messageId > 0) {
                $imageUrl = Yii::app()->request->baseUrl . $model->$imageField;
                echo "<img src=" . $imageUrl . " style=margin-bottom:30px; />";
            }
            ?>
    <div class="item">
          <?php echo "信息描述"; ?>
    </div>
        <?php echo $form->textArea($model, $descriptionField, array('placeholder' => '信息描述...')); ?>
</div>
<?php $this->endWidget(); ?>
<div data-role="controlgroup" data-type="horizontal" style="padding:1px;">
  <button data-role="button" onclick="javascript:send<?php echo $messageId; ?>()">保存</button>   
  <button data-role="button">取消</button>
 </div>
<div data-role="footer" data-position="fixed" data-theme="a" class="ui-footer ui-bar-a ui-footer-fixed slideup ui-fixed-hidden" role="contentinfo">
  <h1 class="ui-title" role="heading" aria-level="1">Copyright billyn.net</h1>
</div>